﻿using MobiSms.DataModel;
using MobiSms.Library;
using MobiSms.Library.Extensions;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using MobiSms.Enums;

namespace MobiSms.ViewModel
{
    public class SerialDetailsViewModel : MainViewModel
    {
        public SerialDetailsViewModel()
        {
            SoapClient.AlterPwdCompleted -= SoapClientUpdatePasswordCompleted;
            SoapClient.AlterPwdCompleted += SoapClientUpdatePasswordCompleted;
        }

        #region Properties
        private string _guid;
        public string Guid
        {
            get { return _guid; }
            set
            {
                _guid = value;
                RaisePropertyChanged(() => Guid);
                if (!IsDesignMode)
                {
                    if (Guid != null && Guid != string.Empty)
                    {
                        GetSerialInfo(Guid);
                    }
                }
            }
        }

        private SerialInfo _selectedSerial;
        public SerialInfo SelectedSerial
        {
            get { return _selectedSerial; }
            set
            {
                _selectedSerial = value;
                RaisePropertyChanged(() => SelectedSerial);
                RaisePropertyChanged(() => DeleteCommand);
            }
        }

        private ObservableCollection<MessageInfo> _itemsOfMessage;
        public ObservableCollection<MessageInfo> ItemsOfMessage
        {
            get
            {
                return _itemsOfMessage;
            }
            set
            {
                _itemsOfMessage = value;
                RaisePropertyChanged(() => ItemsOfMessage);
            }
        }

        private string _newpassword = string.Empty;
        public string NewPassword
        {
            get { return _newpassword; }
            set
            {
                _newpassword = value;
                RaisePropertyChanged(() => NewPassword);
                RaisePropertyChanged(() => UpdatePasswordCommand);
            }
        }

        private string _newSign = string.Empty;
        public string NewSign
        {
            get { return _newSign; }
            set
            {
                _newSign = value;
                RaisePropertyChanged(() => NewSign);
                RaisePropertyChanged(() => UpdateSignatureCommand);
            }
        }
        #endregion

        #region Method & Events
        private void GetSerialInfo(string guid)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                SelectedSerial = Database.CollectionOfSerialNumbers.FirstOrDefault(s => s.Guid == guid);
                if (SelectedSerial != null)
                {
                    SelectedSerial.GetBalance();
                    GetMessageCollection(SelectedSerial.SerialNumber);
                    NewSign = SelectedSerial.Signature;
                }
                else
                {
                    NavigateBack();
                }
            });
        }

        private void GetMessageCollection(string serial)
        {
            IEnumerable<MessageInfo> items = Database.CollectionOfMessages.Where(m => m.SerialNumber == serial);
            ItemsOfMessage = new ObservableCollection<MessageInfo>(items);
        }

        private void Remove(SerialInfo entity)
        {

            Database.CollectionOfSerialNumbers.DeleteOnSubmit(entity);

            Submit();

            NavigateBack();
        }

        private void UpdateSign()
        {
            SerialInfo item = Database.CollectionOfSerialNumbers.FirstOrDefault(s => s.Guid == Guid);
            item.Signature = NewSign;
            if (Submit())
            {
                MessageBox.Show(Application.Current.FindResource<string>(LanguageKeys.IDS_NOTIFICATION_UPDATE_SUCCEED));
            }
            else
            {
                MessageBox.Show(Application.Current.FindResource<string>(LanguageKeys.IDS_NOTIFICATION_UPDATE_FAILD));
            }
        }

        private void SoapClientUpdatePasswordCompleted(object sender, AlterPwdCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result.code == 0)
                {
                    SerialInfo item = Database.CollectionOfSerialNumbers.FirstOrDefault(s => s.Guid == Guid);
                    item.Password = NewPassword;
                    Submit();

                    NewPassword = string.Empty;
                }
                else
                {
                    ShowResult<ResultCodeValue>(e.Result.code);
                    //MessageBox.Show(((ResultCodeValue)e.Result.code).ToString());
                }
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
        }
        #endregion

        public ICommand DeleteCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return SelectedSerial != null; },
                    ExecuteCallback = delegate
                    {
                        if (MessageBox.Show(Application.Current.FindResource<string>(LanguageKeys.IDS_NOTIFICATION_DELETE_COMFIRM_BODY), Application.Current.FindResource<string>(LanguageKeys.IDS_NOTIFICATION_DELETE_COMFIRM_TITLE), MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                        {
                            Remove(SelectedSerial);
                        }
                    }
                };
            }
        }

        public ICommand UpdatePasswordCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return NewPassword.Length == 6; },
                    ExecuteCallback = delegate
                    {
                        SoapClient.AlterPwdAsync(SelectedSerial.SerialNumber, SelectedSerial.Password, NewPassword);
                    }
                };
            }
        }

        public ICommand UpdateSignatureCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return SelectedSerial != null && NewSign != SelectedSerial.Signature && NewSign.Length > 0; },
                    ExecuteCallback = delegate
                    {
                        UpdateSign();
                    }
                };
            }
        }
    }
}
